// 用户数据模块
import http from '@/shopro/request/index'
import store from '@/shopro/store'
import tools from '@/shopro/utils/tools'
import wechat from '@/shopro/wechat/wechat'
import share from '@/shopro/share'
import {router} from '@/shopro/router'
const state = {
	token: uni.getStorageSync("token") || "",
	isLogin: uni.getStorageSync("isLogin") || false, // 是否登陆
	userInfo: uni.getStorageSync("userInfo") || {}, // 用户信息
	agentInfo: {}, //分销商信息
	userData: {}, //用户其他相关信息
	subscribeMessageIdsMap: [], //小程序订阅消息模板ids
	authType: '' // smsLogin:手机号登录注册, accountLogin:密码登录, forgotPwd:忘记密码, changePwd:修改密码, bindMobile:绑定手机号
}

const getters = {
	token: state => state.token,
	isLogin: state => state.isLogin,
	userInfo: state => state.userInfo,
	agentInfo: state => state.agentInfo,
	userOtherData: state => state.userData,
	authType: state => state.authType,
	subscribeMessageIdsMap: state => state.subscribeMessageIdsMap
}

const actions = {
	// 获取用户信息
	getUserInfo({
		commit,
		dispatch,
		getters,
		state
	}, token = '') {
		return new Promise((resolve, reject) => {
			token && uni.setStorageSync('token', token);
			http('login.userInfo').then(res => {
				let lastLoginStatus = getters.isLogin;
				if(res.data.gender == '1'){
					res.data.gendername = '男'
				}
				if(res.data.gender == '2'){
					res.data.gendername = '女'
				}
				if(res.data.gender == '3'){
					res.data.gendername = '保密'
				}
				commit('userInfo', res.data);
				commit('isLogin', true);
				dispatch('showAuthModal');
				!lastLoginStatus && share.setShareInfo();
				// 存在分享信息 添加分享记录
				let spm = uni.getStorageSync('spm');
				if (spm) {
					http('common.shareAdd', {
						spm: spm
					});
					uni.removeStorageSync('spm');
				}
				resolve(res.data)
			}).then(() => {
				token;
			})
				.catch(e => {
					reject(e)
				})
		})
	},
	// 获取分销商身份信息
	getAgent({
		commit
	}) {
		return new Promise((resolve, reject) => {
			http('commission.auth').then(res => {
				if (res.code === 1) {
					commit('AGENT_INFO', res.data.data);
				}
				resolve(res)
			}).catch(e => {
				reject(e)
			});
		})
	},


	// 自动登录
	async autoLogin({
		getters,
		dispatch
	}) {
		if (getters.initWechat?.autologin && !getters.isLogin) { // 微信开启自动登录 并且当前未登录，进入自动登录流程
			let token = '';

			// #ifdef H5
			wechat.login();
			// #endif

			// #ifdef MP-WEIXIN
			// token = await wechat.getWxMiniProgramSessionKey(true)
			// #endif

			token && await dispatch('getUserInfo', token);
		} else if (getters.isLogin) { // 已经登录，直接获取用户信息
			await dispatch('getUserInfo');
		}
		share.setShareInfo();
		// 初始化小程序session_key
		// #ifdef MP-WEIXIN
		if (!getters.initWechat?.autologin) {
			// await wechat.getWxMiniProgramSessionKey(false);
		}
		// #endif
	},

	// 登录跳转
	showAuthModal({
		commit
	}, type) {
		if(type){
			router.push({
				path: '/pages/public/login',
				query: {
				}
			})
		}else{
			uni.navigateBack()
		}
	},
	// 退出登录
	logout({
		commit,
		dispatch
	}) {
		uni.removeStorageSync('token');
		uni.removeStorageSync('userInfo')
		commit('token', "");
		uni.removeStorageSync('chatSessionId');
		commit('isLogin', false);
		commit('userInfo', {});
		commit('CART_LIST', []);
		commit('USER_DATA', {});
	},
	
}

const mutations = {
	token(state, payload) {
		state.token = payload;
		uni.setStorageSync("token", payload);
	},
	// 登录态
	isLogin(state, data) {
		state.isLogin = data;
		uni.setStorageSync('isLogin', data);
	},
	// 用户信息
	userInfo(state, data) {
		state.userInfo = data;
		uni.setStorageSync("userInfo", data);
	},
	USER_DATA(state, data) {
		state.userData = data;
	},
}



export default {
	state,
	mutations,
	actions,
	getters
}
